package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.Coupon;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Coupon entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Coupon
 */
public class CouponDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(CouponDAO.class);
    // property constants
    public static final String COUPON_NAME = "couponName";
    public static final String COUPON_TIPS = "couponTips";
    public static final String COUPON_TYPE = "couponType";
    public static final String PARAM1 = "param1";
    public static final String PARAM2 = "param2";
    public static final String COUPON_NUMBER = "couponNumber";

    public Coupon save(Coupon transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<Coupon> saveList(List<Coupon> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Coupon temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(Coupon persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Coupon findById(java.lang.String id)
    {
        log.debug("getting Coupon instance with id: " + id);
        try
        {
            Coupon instance = (Coupon) getSession().get("com.qianhui.pojo.Coupon", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Coupon instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Coupon as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByCouponName(Object couponName)
    {
        return findByProperty(COUPON_NAME, couponName);
    }

    public List findByCouponTips(Object couponTips)
    {
        return findByProperty(COUPON_TIPS, couponTips);
    }

    public List findByCouponType(Object couponType)
    {
        return findByProperty(COUPON_TYPE, couponType);
    }

    public List findByParam1(Object param1)
    {
        return findByProperty(PARAM1, param1);
    }

    public List findByParam2(Object param2)
    {
        return findByProperty(PARAM2, param2);
    }

    public List findByCouponNumber(Object couponNumber)
    {
        return findByProperty(COUPON_NUMBER, couponNumber);
    }

    public List findAll()
    {
        log.debug("finding all Coupon instances");
        try
        {
            String queryString = "from Coupon";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSize(int index, int pageSize)
    {
        try
        {
            String sql = "from Coupon c order by c.couponCreateTime desc";
            Query query = getSession().createQuery(sql);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCount()
    {
        try
        {
            String sql = "select count(*) from Coupon c";
            Query query = getSession().createQuery(sql);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}